import liepin
from random import randrange
from flask import Flask, request
from flask import render_template
import pandas as pd
from jinja2 import Markup, Environment, FileSystemLoader
from pyecharts.globals import CurrentConfig
from pyecharts import options as opts
from pyecharts.charts import Bar

CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader("./templates"))
app = Flask(__name__, static_folder="templates")


# 配置路由
@app.route("/")
def index():
    return render_template("index.html")


# 过渡页面路由
@app.route("/wait", methods=["POST"])
def wait():
    global job, area, table_html, output_time
    job = request.form["job"]
    area = request.form["area"]
    # 读取数据
    liepinData = liepin.getData(area, job)
    key, output_time, new_df = liepinData
    # 将表格数据转换为HTML格式
    table_html = new_df.to_html()
    time_remaining = 5
    return render_template("wait.html", time_remaining=time_remaining)


# 数据源页面
@app.route("/result")
def result():
    # 进行全局变量的存储
    global job, area, table_html
    return render_template("result.html", table=table_html, job=job, area=area)


# 薪资页面
@app.route("/salary")
def salary():
    return render_template("salary.html")


# 柱状图
@app.route("/salaryDataA")
def salaryDataA():
    global job, area, output_time
    c, s = liepin.salaryData(job, area, output_time)
    return c.dump_options_with_quotes()


# 散点图
@app.route("/salaryDataB")
def salaryDataB():
    global job, area, output_time
    c, s = liepin.salaryData(job, area, output_time)
    return s.dump_options_with_quotes()


# 技能/类型页面(词云图)
@app.route("/skill")
def skill():
    return render_template("skill.html")


@app.route("/skillData")
def skillWordCloud():
    global job, area, output_time
    c = liepin.skillData(job, area, output_time)
    return c.dump_options_with_quotes()


# 岗位名称页面(词云图)
@app.route("/jobName")
def jobName():
    return render_template("jobName.html")


@app.route("/jobNameData")
def jobNameWordCloud():
    global job, area, output_time
    c = liepin.jobData(job, area, output_time)
    return c.dump_options_with_quotes()


# 地区分布页面(地图)
@app.route("/area")
def area():
    return render_template("area.html")


@app.route("/areaData")
def get_bar_chart():
    global job, area, output_time
    c,s = liepin.areaData(job, area, output_time)
    return c.dump_options_with_quotes()

@app.route("/areaData2")
def get_bar_chart2():
    global job, area, output_time
    c,s = liepin.areaData(job, area, output_time)
    return s.dump_options_with_quotes()


# 行业薪资排序图
@app.route("/trades")
def trades():
        return render_template("trades.html")

@app.route("/tradeSalary")
def hangyeSalary():
    global job, area, output_time
    c = liepin.hangyeSalary(job, area, output_time)
    return c.dump_options_with_quotes()

if __name__ == "__main__":
    app.run(debug=True)
